﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using WebAppPlanProduccion.Code.Security;
using Web.Framework;

using PlanProd.Contracts.Search;
using PlanProd.Contracts;
using Web.Framework.Svc;

namespace WebAppPlanProduccion.App
{
    public partial class ProveedorList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                EsPaginaQueRecurdaFiltros = true;
                HasPermissionToSeeMe(PlanProdSecurity.PROVEEDOR_VER);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        LinkButton sincronizarButton = new LinkButton();        
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            sincronizarButton.ID = "BoCommandSincronizar";
            sincronizarButton.Text = "Sincronizar Proveedores";
            sincronizarButton.Click += new EventHandler(lnkSincronizar_Click);
            sincronizarButton.OnClientClick = "javascript:alert('Los proveedores se sincronizarán desde un sistema externo. Consulte la pantalla de sincronización para ver el resultado del proceso.'); return true;";
            actionButtons.Add(sincronizarButton);
         
            base.LoadContextActions(actionButtons);
        }

        protected override void ApplySecurity()
        {
            sincronizarButton.Visible =
            this.lnkSincronizar.Visible = HasPermissionFor(PlanProdSecurity.PROVEEDOR_SINCRONIZAR);
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkSincronizar_Click(object sender, EventArgs e)
        {
            try
            {
                Proveedor search = new Proveedor();
                search.ProveedorIdExterno = Convert.ToInt32(this.txtCodigoProveedor.Text);
                var proveedores = WebServiceHelper.SincronizarProveedores(search);

                this.GridProveedores.DataSource = proveedores;
                this.GridProveedores.DataBind();

                pnlResults.Visible = true;

                base.masterPage.ShowMessage(string.Format("Se sincronizaron {0} proveedores", proveedores.Count), WebMessageType.Notification);                
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridProveedores_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.GridProveedores.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridProveedores_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }

        protected void GridProveedores_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        private void Buscar()
        {
            RecordarFiltrosSeleccionados();
            ServiceOptions searchOptions = new ServiceOptions();
            searchOptions.PageNumber = GridProveedores.PageIndex;
            searchOptions.PageSize = GridProveedores.PageSize;
            var securityContext = GetSecurityContextForDestinos();
            searchOptions.IsAdmin = BaseWebPage.IsAdmin;
            searchOptions.Operations = securityContext.Operations;

            SearchProveedorCriteria productSearchCriteria = new SearchProveedorCriteria();
            productSearchCriteria.EmpresaId = BaseWebPage.CompanyId;
            productSearchCriteria.RazonSocial = this.txtRazonSocial.Text;

            List<Proveedor> proveedores = WebServiceHelper.GetProveedores(searchOptions, productSearchCriteria);

            this.GridProveedores.DataSource = proveedores;
            this.GridProveedores.DataBind();

            pnlResults.Visible = true;
        }
    }
}